package cn.bdqn.t354.controller;

import cn.bdqn.t354.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String hello(Model model){
        model.addAttribute("msg","hello thymeleaf");
        return "hello";
    }

    //变量表达式
    @RequestMapping("/expression")
    public String expression(Model model, HttpServletRequest request,
                             HttpSession session){
        //添加数据到Model
        model.addAttribute("name","张三");
        model.addAttribute("user",new User(1,"李四","男",18));

        //添加数据到request
        request.setAttribute("username","李四");
        //添加数据到session
        session.setAttribute("username","王五");

        //添加时间到Model
        model.addAttribute("date",new Date());

        return "demo/expression";
    }

    //变量表达式
    @RequestMapping("/expression2")
    public String expression2(Model model){
        //添加数据到Model
        model.addAttribute("user",new User(1,"李四","男",18));
        return "demo/expression2";
    }

    //变量表达式
    @RequestMapping("/link")
    public String link(Model model){
        model.addAttribute("user",new User(1,"李四","男",18));
        return "demo/link";
    }

    //变量表达式
    @RequestMapping("/insert")
    @ResponseBody
    public User insert(User user,Model model){
        return user;
    }

    //使用模板的属性
    @RequestMapping("/properties")
    public String properties(Model model){
        model.addAttribute("method","post");
        model.addAttribute("name","username");
        model.addAttribute("value","zhangsan");
        model.addAttribute("color","color:red");
        return "demo/properties";
    }

    @RequestMapping("/ifunless")
    public String ifunless(Model model){
        model.addAttribute("age",15);
        return "demo/ifunless";
    }

    @RequestMapping("/switch2")
    public String switch2(Model model){
        model.addAttribute("sex","男");
        return "demo/switch";
    }

    @RequestMapping("/each")
    public String each(Model model){
        List<User> list=new ArrayList<>();
        list.add(new User(1,"张三","男",18));
        list.add(new User(2,"李四","男",19));
        list.add(new User(3,"王五","男",20));

        model.addAttribute("list",list);

        return "demo/each";
    }

    @RequestMapping("/frame")
    public String frame(){
        return "demo/frame";
    }

    @RequestMapping("/list")
    public String list(Model model){
        List<User> list=new ArrayList<>();
        list.add(new User(1,"张三","男",18));
        list.add(new User(2,"李四","男",19));
        list.add(new User(3,"王五","男",20));

        model.addAttribute("list",list);
        return "demo/user-list";
    }

    //内联 text
    @RequestMapping("/inline")
    public String inline(Model model){
        model.addAttribute("name","张三");
        return "demo/inline";
    }

}
